package com.aqie.appletserver.controller.baiduapi;

import com.aqie.appletserver.bean.request.WordRequest;
import com.aqie.appletserver.bean.response.*;
import com.aqie.appletserver.common.response.ResponseModel;
import com.aqie.appletserver.service.IBaiduApiService;
import com.aqie.appletserver.service.IUploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("baidu/picture")
@Slf4j
public class PictureRecognitionController {
    @Autowired
    private IBaiduApiService baiduApiService;

    @Autowired
    private IUploadService uploadService;

    // 人像动漫化
    private String SELFIE_ANIME = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime";

    // 黑白图像上色
    private String COLOURIZE = "https://aip.baidubce.com/rest/2.0/image-process/v1/colourize";

    // 图片无损放大
    private String IMG_QUANTITY_ENHANCE = "https://aip.baidubce.com/rest/2.0/image-process/v1/image_quality_enhance";

    // 图片对比度增强
    private String CONTRAST_ENHANCE = "https://aip.baidubce.com/rest/2.0/image-process/v1/contrast_enhance";

    // 图像去雾
    private String DEHAZE = "https://aip.baidubce.com/rest/2.0/image-process/v1/dehaze";

    // 通用文字识别
    private String WORD = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";
    private String WORD_ACCURATE = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic";


    HashMap<String,String> map = new HashMap<>();
    {
        map.put("SELFIE_ANIME", SELFIE_ANIME);
        map.put("COLOURIZE", COLOURIZE);
        map.put("IMG_QUANTITY_ENHANCE", IMG_QUANTITY_ENHANCE);
        map.put("CONTRAST_ENHANCE", CONTRAST_ENHANCE);
        map.put("DEHAZE", DEHAZE);

        map.put("WORD", WORD);
        map.put("WORD_ACCURATE", WORD_ACCURATE);
    }


    @PostMapping("identifyFruitsAndVegetables")
    public ResponseModel<FruitVegetablesResponse> identifyFruitsAndVegetables(HttpServletRequest request) throws Exception {
        Map<String, String> resMap = uploadService.uploadFile(request);
        FruitVegetablesResponse resp = baiduApiService.identifyFruitsAndVegetables(resMap.get("localSavePath"));
        return ResponseModel.success(resp);
    }

    // 通用图片识别
    // {{tomcat}}/baidu/picture/commonObjectAndSceneRecognition2
    @PostMapping("commonObjectAndSceneRecognition")
    public ResponseModel<CommonPictureResponse> commonObjectAndSceneRecognition(HttpServletRequest request) throws Exception {
        Map<String, String> resMap = uploadService.uploadFile(request);
        CommonPictureResponse resp = baiduApiService.commonObjectAndSceneRecognition(resMap.get("localSavePath"));
        return ResponseModel.success(resp);
    }

    // {{tomcat}}/baidu/picture/commonObjectAndSceneRecognition2
    @PostMapping("commonObjectAndSceneRecognition2")
    public ResponseModel<CommonPictureResponse> commonObjectAndSceneRecognition2(HttpServletRequest request) throws Exception {
        Map<String, String> resMap = uploadService.uploadFile(request);
        CommonPictureResponse resp = baiduApiService.commonObjectAndSceneRecognition2(resMap.get("localSavePath"));
        return ResponseModel.success(resp);
    }

    @PostMapping("handleImage/{type}")
    public ResponseModel<PictureResponse> handleImage(HttpServletRequest request, @PathVariable("type") String type) throws Exception {
        Map<String, String> resMap = uploadService.uploadFile(request);
        PictureResponse resp = baiduApiService.handleImage(resMap.get("localSavePath"), map.get(type));
        return ResponseModel.success(resp);
    }

    @PostMapping("receipt")
    public ResponseModel<ReceiptResponse> receipt(HttpServletRequest request) throws Exception {
        Map<String, String> resMap = uploadService.uploadFile(request);
        ReceiptResponse resp = baiduApiService.receipt(resMap.get("localSavePath"));
        return ResponseModel.success(resp);
    }

    @PostMapping("redWine")
    public ResponseModel<WineResponse> redWine(HttpServletRequest request) throws Exception {
        Map<String, String> resMap = uploadService.uploadFile(request);
        WineResponse resp = baiduApiService.wine(resMap.get("localSavePath"));
        return ResponseModel.success(resp);
    }

    @PostMapping("food")
    public ResponseModel<FoodResponse> food(HttpServletRequest request) throws Exception {
        Map<String, String> resMap = uploadService.uploadFile(request);
        FoodResponse resp = baiduApiService.food(resMap.get("localSavePath"));
        return ResponseModel.success(resp);
    }

    @PostMapping("word/{type}")
    public ResponseModel<WordResponse> word(HttpServletRequest request, WordRequest wordRequest, @PathVariable("type") String type) throws Exception {
        Map<String, String> resMap = uploadService.uploadFile(request);

        WordResponse resp = baiduApiService.word(map.get(type),wordRequest, resMap.get("localSavePath"));
        return ResponseModel.success(resp);
    }

}
